package com.yunxi.controller.market;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.remoting.exchange.Response;
import com.alibaba.fastjson.JSONObject;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.CurrentUser;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.dealer.pojo.PartnerVO;
import com.yunxi.market.pojo.*;
import com.yunxi.market.service.MarketActivityDubboService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 市场活动主表 前端控制器
 * </p>
 *
 * @author
 * @since 2019-08-17
 */
@RestController
@RequestMapping("/admin/marketactivity")
@Api(tags = "pms:marketactivity", description = "市场活动主表接口")
public class MarketActivityController {

    private static final Logger logger = LoggerFactory.getLogger(MarketActivityController.class);

    @Autowired
    MarketActivityDubboService marketActivityDubboService;

    /**
     * 市场活动经销商申请列表主数据查询分页
     *
     * @param vo
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/queryMarketActivityForPage")
    @ApiOperation(value = "市场活动经销商申请列表主数据查询分页")
    public ApiResult queryMarketActivityForPage(MarketActivityQueryReqVO vo, Integer pageNum, Integer pageSize)
        throws Exception {
        return ResponseUtil.ok(marketActivityDubboService.queryMarketActivityForPage(vo, pageNum, pageSize));
    }

    /**
     * 市场活动厂商审核列表主数据查询分页
     *
     * @param vo
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/queryMarketActivityForApprovePage")
    @ApiOperation(value = "市场活动厂商审核列表主数据查询分页")
    public ApiResult queryMarketActivityForApprovePage(MarketActivityQueryReqVO vo, Integer pageNum, Integer pageSize)
        throws Exception {
        return ResponseUtil.ok(marketActivityDubboService.queryMarketActivityForApprovePage(vo, pageNum, pageSize));
    }

    /**
     * 市场活动明细查询
     *
     * @param vo
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/queryMarketActivityDetails")
    @ApiOperation(value = "市场活动明细查询")
    public ApiResult queryMarketActivityDetails(MarketActivityDetailsQueryReqVO vo) {
        return ResponseUtil.ok(marketActivityDubboService.queryMarketActivityDetails(vo));
    }

    /**
     * 点击提报查看市场活动明细
     *
     * @param vo
     * @return
     */
    @GetMapping("/queryMarketActivityDetailsByToPut")
    @ApiOperation(value = "市场活动明细查询")
    public ApiResult<MarketActivityDetailsQueryRespVO> queryMarketActivityDetailsByToPut(
        MarketActivityDetailsQueryReqVO vo) {
        return ResponseUtil.ok(marketActivityDubboService.queryMarketActivityDetailsByToPut(vo));
    }

    /**
     * 渠道商保存市场活动数据
     *
     * @return
     */
    @PostMapping("/saveMarketActivityByPartner")
    @ApiOperation(value = "渠道商保存市场活动")
    public ApiResult<Boolean> saveMarketActivityByPartner(@RequestBody MarketActivitySaveReqVO vo,
        @CurrentUser LoginInfo user) {
        Object partnerVO = (Object) user.getPartner();
        if (partnerVO != null) {
            //
            //表示当前用户是渠道商
            partnerVO = JSONObject.parseObject(JSONObject.toJSONString(partnerVO), PartnerVO.class);
            vo.getMarketActivity().setPartnerCode(((PartnerVO) partnerVO).getPartnerCode());
        } else {
            //
            //表示当前用户是主机厂
            return ResponseUtil.fail(401, "当前用户不是渠道商用户，请使用渠道商账户登录！");
        }

        return ResponseUtil.ok(marketActivityDubboService.saveMarketActivity(vo));
    }

    /**
     * 渠道商保存市场活动数据
     *
     * @return
     */
    @PostMapping("/saveMarketActivityByApprove")
    @ApiOperation(value = "厂商保存市场活动")
    public ApiResult<Boolean> saveMarketActivityByApprove(@RequestBody MarketActivitySaveReqVO vo,
        @CurrentUser LoginInfo user) {
        return ResponseUtil.ok(marketActivityDubboService.saveMarketActivity(vo));
    }

    @PostMapping("/toPut")
    @ApiOperation(value = "提报")
    public ApiResult<Boolean> toPut(@RequestBody MarketActivityToPutReqVO vo) {
        return ResponseUtil.ok(marketActivityDubboService.toPut(vo));
    }


    /**
     * 修改并保存市场活动数据
     *
     * @param vo
     * @param user
     * @return
     */
    @PostMapping("/updateMarketActivity")
    @ApiOperation(value = "修改并保存市场活动数据")
    public ApiResult<Boolean> updateMarketActivity(@RequestBody MarketActivityModifyReqVO vo,
        @CurrentUser LoginInfo user) {
        return ResponseUtil.ok(marketActivityDubboService.updateMarketActivity(vo));
    }

    @PostMapping("/audit")
    @ApiOperation(value = "审核")
    public ApiResult<Boolean> audit(@RequestBody MarketActivityAuditReqVO vo, @CurrentUser LoginInfo user) {
        Object partnerVO = (Object) user.getPartner();
        if (partnerVO != null) {
            //
            //表示当前用户是主机厂
            return ResponseUtil.fail(401, "当前用户不是主机厂用户，请使用主机厂用户登录！");
        }

        vo.setAuditBy(user.getUserCode());

        return ResponseUtil.ok(marketActivityDubboService.audit(vo));
    }


    @PostMapping("/delMarketActivityItem")
    @ApiOperation(value = "删除活动项")
    public ApiResult<Boolean> delMarketActivityItem(@RequestBody MarketActivityItemDelReqVO vo) {
        return ResponseUtil.ok(marketActivityDubboService.delMarketActivityItem(vo));
    }
}
